﻿#region zh-CHS 2006 - 2007 DemoSoft 团队 | en 2006 - 2007 DemoSoft Team

//     NOTES
// ---------------
//
// This file is a part of the MMOSE(Massively Multiplayer Online Server Engine) for .NET.
//
//                              2006-2007 DemoSoft Team
//
//
// First Version : by H.Q.Cai - mailto:caihuanqing@hotmail.com

/***************************************************************************
 *
 *   This program is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU Lesser General Public License as published
 *   by the Free Software Foundation; either version 2.1 of the License, or
 *   (at your option) any later version.
 *
 ***************************************************************************/

#region zh-CHS 包含名字空间 | en Include namespace
using System;
using System.Text;
using System.Threading;
using System.Collections;
using System.Collections.Generic;
using Demo.Mmose.Core.Item;
using Demo.Mmose.Core.Common;
using Demo.Mmose.Core.Creature;
using Demo.Mmose.Core.Network;
using Demo.Mmose.Core.Util;
using Demo.Mmose.Core.Character;
#endregion

namespace Demo.Mmose.Core.Map
{
    /// <summary>
    /// 
    /// </summary>
    internal class ItemEnumerator<T> : IEnumerator<T> where T : BaseItem
    {
        #region zh-CHS 构造和初始化和清理 | en Constructors and Initializers and Dispose
        /// <summary>
        /// 
        /// </summary>
        /// <param name="map"></param>
        /// <param name="bounds"></param>
        /// <param name="type"></param>
        private ItemEnumerator( List<BaseItem[]> itemArrayList )
        {
            m_ItemArrayList = itemArrayList;
        }
        #endregion

        #region zh-CHS 共有静态方法 | en Public Static Methods
        /// <summary>
        /// 
        /// </summary>
        /// <param name="map"></param>
        /// <param name="bounds"></param>
        /// <param name="type"></param>
        /// <returns></returns>
        public static ItemEnumerator<T> Instantiate( List<BaseItem[]> itemArrayList )
        {
            return new ItemEnumerator<T>( itemArrayList );
        }
        #endregion

        #region zh-CHS 接口实现 | en Interface Implementation
        #region zh-CHS 私有成员变量 | en Private Member Variables
        /// <summary>
        /// 
        /// </summary>
        private int m_ArrayIndex = 0;
        /// <summary>
        /// 
        /// </summary>
        private int m_ListIndex = 0;
        /// <summary>
        /// 
        /// </summary>
        private List<BaseItem[]> m_ItemArrayList = null;
        #endregion
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public bool MoveNext()
        {
            if ( m_ListIndex >= m_ItemArrayList.Count )
                return false;

            if ( m_ArrayIndex >= m_ItemArrayList[m_ListIndex].Length )
                return false;

            do
            {
                m_Current = m_ItemArrayList[m_ListIndex][m_ArrayIndex] as T;
                if ( m_Current == null )
                {
                    m_ArrayIndex++;

                    if ( m_ArrayIndex >= m_ItemArrayList[m_ListIndex].Length )
                    {
                        m_ListIndex++;

                        if ( m_ListIndex >= m_ItemArrayList.Count )
                        {
                            m_Current = null;
                            return false;
                        }
                    }
                }
            } while ( m_Current == null );

            return true;
        }

        /// <summary>
        /// 
        /// </summary>
        public void Reset()
        {
            m_ListIndex = 0;
            m_ArrayIndex = 0;
            m_Current = null;
        }

        #region zh-CHS 私有成员变量 | en Private Member Variables
        /// <summary>
        /// 只在Release(...)中改变
        /// </summary>
        private int m_bIsRelease = 0;
        #endregion
        /// <summary>
        /// 
        /// </summary>
        public void Release()
        {
            if ( Interlocked.Exchange( ref m_bIsRelease, 1 ) == 1 )
                return;
        }

        /// <summary>
        /// 
        /// </summary>
        public void Dispose()
        {
            Release();
        }
        #endregion

        #region zh-CHS 接口实现属性 | en Interface Implementation Properties
        #region zh-CHS 私有成员变量 | en Private Member Variables
        /// <summary>
        /// 当前数据
        /// </summary>
        private T m_Current = null;
        #endregion
        /// <summary>
        /// 
        /// </summary>
        public T Current
        {
            get { return m_Current; }
        }

        /// <summary>
        /// 
        /// </summary>
        object IEnumerator.Current
        {
            get { return m_Current; }
        }
        #endregion
    }

    /// <summary>
    /// 
    /// </summary>
    internal class CreatureEnumerator<T> : IEnumerator<T> where T : BaseCreature
    {
        #region zh-CHS 构造和初始化和清理 | en Constructors and Initializers and Dispose
        /// <summary>
        /// 
        /// </summary>
        /// <param name="map"></param>
        /// <param name="bounds"></param>
        /// <param name="type"></param>
        private CreatureEnumerator( List<BaseCreature[]> creatureArrayList )
        {
            m_CreatureArrayList = creatureArrayList;
        }
        #endregion

        #region zh-CHS 共有静态方法 | en Public Static Methods
        /// <summary>
        /// 
        /// </summary>
        /// <param name="map"></param>
        /// <param name="bounds"></param>
        /// <param name="type"></param>
        /// <returns></returns>
        public static CreatureEnumerator<T> Instantiate( List<BaseCreature[]> creatureArrayList )
        {
            return new CreatureEnumerator<T>( creatureArrayList );
        }
        #endregion

        #region zh-CHS 接口实现 | en Interface Implementation
        #region zh-CHS 私有成员变量 | en Private Member Variables
        /// <summary>
        /// 
        /// </summary>
        private int m_ArrayIndex = 0;
        /// <summary>
        /// 
        /// </summary>
        private int m_ListIndex = 0;
        /// <summary>
        /// 
        /// </summary>
        private List<BaseCreature[]> m_CreatureArrayList = null;
        #endregion
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public bool MoveNext()
        {
            if ( m_ListIndex >= m_CreatureArrayList.Count )
                return false;

            if ( m_ArrayIndex >= m_CreatureArrayList[m_ListIndex].Length )
                return false;

            do
            {
                m_Current = m_CreatureArrayList[m_ListIndex][m_ArrayIndex] as T;
                if ( m_Current == null )
                {
                    m_ArrayIndex++;

                    if ( m_ArrayIndex >= m_CreatureArrayList[m_ListIndex].Length )
                    {
                        m_ListIndex++;

                        if ( m_ListIndex >= m_CreatureArrayList.Count )
                        {
                            m_Current = null;
                            return false;
                        }
                    }
                }
            } while ( m_Current == null );

            return true;
        }

        /// <summary>
        /// 
        /// </summary>
        public void Reset()
        {
            m_ListIndex = 0;
            m_ArrayIndex = 0;
            m_Current = null;
        }

        #region zh-CHS 私有成员变量 | en Private Member Variables
        /// <summary>
        /// 只在Release(...)中改变
        /// </summary>
        private int m_bIsRelease = 0;
        #endregion
        /// <summary>
        /// 
        /// </summary>
        public void Release()
        {
            if ( Interlocked.Exchange( ref m_bIsRelease, 1 ) == 1 )
                return;
        }

        /// <summary>
        /// 
        /// </summary>
        public void Dispose()
        {
            Release();
        }
        #endregion

        #region zh-CHS 接口实现属性 | en Interface Implementation Properties
        #region zh-CHS 私有成员变量 | en Private Member Variables
        /// <summary>
        /// 当前数据
        /// </summary>
        private T m_Current = null;
        #endregion
        /// <summary>
        /// 
        /// </summary>
        public T Current
        {
            get { return m_Current; }
        }

        /// <summary>
        /// 
        /// </summary>
        object IEnumerator.Current
        {
            get { return m_Current; }
        }
        #endregion
    }

    /// <summary>
    /// 
    /// </summary>
    internal class CharacterEnumerator<T> : IEnumerator<T> where T : BaseCharacter
    {
        #region zh-CHS 构造和初始化和清理 | en Constructors and Initializers and Dispose
        /// <summary>
        /// 
        /// </summary>
        /// <param name="map"></param>
        /// <param name="bounds"></param>
        /// <param name="type"></param>
        private CharacterEnumerator( List<BaseCharacter[]> characterArrayList )
        {
            m_CharacterArrayList = characterArrayList;
        }
        #endregion

        #region zh-CHS 共有静态方法 | en Public Static Methods
        /// <summary>
        /// 
        /// </summary>
        /// <param name="map"></param>
        /// <param name="bounds"></param>
        /// <param name="type"></param>
        /// <returns></returns>
        public static CharacterEnumerator<T> Instantiate( List<BaseCharacter[]> characterArrayList )
        {
            return new CharacterEnumerator<T>( characterArrayList );
        }
        #endregion

        #region zh-CHS 接口实现 | en Interface Implementation
        #region zh-CHS 私有成员变量 | en Private Member Variables
        /// <summary>
        /// 
        /// </summary>
        private int m_ArrayIndex = 0;
        /// <summary>
        /// 
        /// </summary>
        private int m_ListIndex = 0;
        /// <summary>
        /// 
        /// </summary>
        private List<BaseCharacter[]> m_CharacterArrayList = null;
        #endregion
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public bool MoveNext()
        {
            if ( m_ListIndex >= m_CharacterArrayList.Count )
                return false;

            if ( m_ArrayIndex >= m_CharacterArrayList[m_ListIndex].Length )
                return false;

            do
            {
                m_Current = m_CharacterArrayList[m_ListIndex][m_ArrayIndex] as T;
                if ( m_Current == null )
                {
                    m_ArrayIndex++;

                    if ( m_ArrayIndex >= m_CharacterArrayList[m_ListIndex].Length )
                    {
                        m_ListIndex++;

                        if ( m_ListIndex >= m_CharacterArrayList.Count )
                        {
                            m_Current = null;
                            return false;
                        }
                    }
                }
            } while ( m_Current == null );

            return true;
        }

        /// <summary>
        /// 
        /// </summary>
        public void Reset()
        {
            m_ListIndex = 0;
            m_ArrayIndex = 0;
            m_Current = null;
        }

        #region zh-CHS 私有成员变量 | en Private Member Variables
        /// <summary>
        /// 只在Release(...)中改变
        /// </summary>
        private int m_bIsRelease = 0;
        #endregion
        /// <summary>
        /// 
        /// </summary>
        public void Release()
        {
            if ( Interlocked.Exchange( ref m_bIsRelease, 1 ) == 1 )
                return;
        }

        /// <summary>
        /// 
        /// </summary>
        public void Dispose()
        {
            Release();
        }
        #endregion

        #region zh-CHS 接口实现属性 | en Interface Implementation Properties
        #region zh-CHS 私有成员变量 | en Private Member Variables
        /// <summary>
        /// 当前数据
        /// </summary>
        private T m_Current = null;
        #endregion
        /// <summary>
        /// 
        /// </summary>
        public T Current
        {
            get { return m_Current; }
        }

        /// <summary>
        /// 
        /// </summary>
        object IEnumerator.Current
        {
            get { return m_Current; }
        }
        #endregion
    }
}
#endregion

